﻿Module modInet
    Public Const SITE_MG As String = "http://moygorod-kna.ru/"


    Public Sub WebReadyState(ByRef web As WebBrowser, ByVal url As String, Optional ByVal max_time As Integer = 40000)
        While True
            web.Navigate(url)
            If ReadyStateComplete(web, max_time) = True Then Exit While
        End While
    End Sub
    Public Function WebReadyState(ByRef IE As SHDocVw.InternetExplorer, ByVal url As String, Optional ByVal max_time As Integer = 40000) As String
        IE.Navigate(url)
        While IE.Busy = True Or IE.ReadyState <> 4
            Application.DoEvents()
        End While
        Return IE.Document.Body.innerHTML
    End Function

    Public Function GetResponseFromWin1251ToUTF8(ByRef web As WebBrowser) As String
        Dim stream As System.IO.Stream = web.DocumentStream
        Dim sr As System.IO.StreamReader = New System.IO.StreamReader(stream, Encoding.GetEncoding(1251))
        GetResponseFromWin1251ToUTF8 = sr.ReadToEnd()
        stream.Close()
    End Function




    Public Function ReadyStateComplete(ByRef web As WebBrowser, ByVal max_time As Integer) As Boolean
        Dim t As UInteger
        t = GetTickCount() 'получаем стартовое время в миллисекундах
        On Error GoTo exit_sub
        Do While web.ReadyState <> WebBrowserReadyState.Complete
            frmMain.StatusStripLabel.Text = web.ReadyState
            Threading.Thread.Sleep(500)
            If GetTickCount() - t > max_time Then
                ReadyStateComplete = False
                Exit Function
            End If
            Application.DoEvents()
        Loop
        frmMain.StatusStripLabel.Text = web.ReadyState
        ReadyStateComplete = True
exit_sub:
    End Function



    Public Sub ReadyStateLoaded(ByRef web As WebBrowser)
        On Error GoTo exit_sub
        Do While web.ReadyState <> WebBrowserReadyState.Interactive
            Threading.Thread.Sleep(500)
            Application.DoEvents()
        Loop
exit_sub:
    End Sub



    Public Function GetHTTPResponse(ByRef oHttpXML As MSXML2.XMLHTTP, _
                                    ByVal sURL As String, _
                                    Optional ByVal charset As String = "utf-8", _
                                    Optional ByVal keep_alive As String = "300" _
    ) As Object
        'Создаем объект веб запросов
        If IsNothing(oHttpXML) Then oHttpXML = New MSXML2.XMLHTTP

        On Error Resume Next
        With oHttpXML
            .open("GET", sURL, False)
            'чтобы избежать кеширования
            .setRequestHeader("If-Modified-Since", "Thu, 1 Jan 1970 00:00:00 UTC")
            'прикидываемся оперой
            .setRequestHeader("User-Agent", "Opera/9.80 (Windows NT 5.1) Presto/2.12.388 Version/12.15")
            If charset <> "" Then
                .setRequestHeader("Accept-Charset", charset)
            End If
            If keep_alive <> "" Then
                .setRequestHeader("Keep-Alive", keep_alive)
                .setRequestHeader("Connection", "keep-alive")
            End If
            'если вы сидите за proxy раскомментируйте следующие строки и подставьте верные IP, логин и пароль
            '.setProxy(2, "192.168.100.1:3128")
            '.setProxyCredentials("user", "password")
            .send()
            If CLng(.status) <> 200 Then
                GetHTTPResponse = CLng(.status)
            Else
                GetHTTPResponse = .responseText
            End If
        End With
    End Function
End Module